﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.Reports;
using Microsoft.Office;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace ControllerLayer.Reports
{
    public class InformManager
    {

        public static string currentDocumentSelection = string.Empty;
        public static string GetPathOfReportPartOID(Guid reportPartOID)
        {
            return ReportDataAccess.GetPathOfReportPartOid(reportPartOID);
        }


        public static void ConvertDocxToPdf(string path)
        {
            //Change the path of the .docx file and filename to your file name.

            char[] space = { ' ' };
            path = path.ToString().Trim(space);

            object paramSourceDocPath = path;

            object paramMissing = Type.Missing;

            Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

            Document wordDocument = null;

            //Change the path of the .pdf file and filename to your file name.
            string paramSourceFile = path;
            //paramSourceFile.Replace("docx", "pdf");
            //string paramExportFilePath = path.Replace("docx", "pdf");
            string paramExportFilePath = paramSourceFile.Replace("docx", "pdf");
            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            try
            {
                // Open the source document.
                wordDocument = wordApplication.Documents.Open(
                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);


                // Export it in the specified format.

                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }

            catch (Exception ex)
            {
                // Respond to the error
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            finally
            {
                // Close and release the Document object.
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordDocument = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}
